home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / gnushogi.lha / gnushogi-1.1 / src / gdxbkstats.c < prev    next >
C/C++ Source or Header  |  1993-04-16  |  4KB  |  191 lines

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include "gnushogi.h"
  4. #undef rxx
  5. #undef cxx
  6. #undef scanz
  7. #undef printz
  8.  
  9. #define rxx "ihgfedcba"
  10. #define cxx "987654321"
  11.  
  12. static char pchar[NO_PIECES] =
  13.  { 'P','L','N','S','G','B','R','P','L','N','S','B','R','K' };
  14.  
  15. char xmvstr[12];
  16.  
  17.  
  18. char *
  19. cvt (m,flags)
  20.      unsigned int m;
  21.      short flags;
  22. {
  23.     unsigned int f, t;
  24.     short piece = 0, flag = 0;
  25.     char *s;
  26.     f = m >> 8 & 0x7f;
  27.     t = m & 0xff;
  28. /* algebraic notation */
  29.     if ( f > NO_SQUARES )
  30.       { short piece = f - NO_SQUARES;
  31.         if ( f > NO_PIECES ) f -= NO_PIECES;
  32.         flag = (dropmask | piece );
  33.       }
  34.     if ( t & 0x80 )
  35.       {
  36.         flag |= promote;
  37.         t &= 0x7f;
  38.       }
  39.     s = xmvstr;
  40.     if ( flag & dropmask )
  41.       { 
  42.     *s = pchar[piece]; s++;
  43.         *s = '*'; s++;
  44.         *s = cxx[column (t)]; s++;
  45.         *s = rxx[row (t)]; s++;
  46.       }
  47.     else
  48.       {
  49.         *s = cxx[column (f)]; s++;
  50.         *s = rxx[row (f)]; s++;
  51.         *s = cxx[column (t)]; s++;
  52.         *s = rxx[row (t)]; s++;
  53.         if ( flag & promote )
  54.           {
  55.             *s = '+'; s++;
  56.           }
  57.       }
  58.     if (m & DONTUSE)
  59.       {
  60.     *s = '?'; s++;
  61.       }
  62.     if (flags & LASTMOVE)
  63.       {
  64.     *s = '.'; s++;
  65.       }
  66.     *s = '\0';
  67.     return xmvstr;
  68. }
  69.  
  70. /* #define lts(x) (x>>16) */        /* long to short to convert hashkey to short */
  71.  
  72. #ifdef LONG64
  73. #define lts(x) (((x>>48)&0xfffe)|(x&0x1))
  74. #else
  75. #define lts(x) (((x>>16)&0xfffe)|(x&0x1))
  76. #endif
  77.  
  78. int gfd;
  79. struct gdxadmin
  80. {
  81.     unsigned int bookcount;
  82.     unsigned int booksize;
  83.     unsigned long maxoffset;
  84. } ADMIN;
  85.  
  86. #define N 2
  87. struct gdxdata
  88. {
  89.     unsigned int hashbd;
  90.     unsigned short hashkey;
  91.     unsigned short bmove;
  92.     unsigned short flags;
  93.     unsigned short hint;
  94.     unsigned short count;
  95. } DATA;
  96. void 
  97. usage (char *x)
  98. {
  99.     printf ("usage %s binbookfile [ -h key bd]\n", x);
  100.     exit ();
  101. }
  102.  
  103. int i;
  104. int c1 = 0;
  105. int in = 0;
  106. int max = -9999;
  107. int min = 9999999;
  108. int n = 0;
  109. int sum = 0;
  110. int sumc = 0;
  111. unsigned long key, bd;
  112. int hk = false;
  113. main (argc, argv)
  114.      int argc;
  115.      char **argv;
  116. {
  117.     if (argc == 5)
  118.       {
  119.       if (strcmp (argv[2], "-h") != 0)
  120.           usage (argv[0]);
  121.       key = strtol (argv[3], NULL, 16);
  122.       bd = strtol (argv[4], NULL, 16);
  123.       hk = true;
  124.       }
  125.     else if (argc != 2)
  126.     usage (argv[0]);
  127.     gfd = open (argv[1], O_RDONLY);
  128.     if (gfd >= 0)
  129.       {
  130.       read (gfd, &ADMIN, sizeof (struct gdxadmin));
  131.       printf ("entrysize %d\nbooksize %d\nbookcount %d\nmaxoffset %ld\n", sizeof (struct gdxdata), ADMIN.booksize, ADMIN.bookcount, ADMIN.maxoffset);
  132.       for (i = 0; i < ADMIN.booksize; i++)
  133.         {
  134.         if (0 > read (gfd, &DATA, sizeof (struct gdxdata)))
  135.           {
  136.               perror ("fread");
  137.               exit (1);
  138.           }
  139.         if (hk)
  140.           {  
  141. #if 1 
  142.               if (DATA.count)
  143.             {
  144.                 printf ("bd = %ld key = %ld: ",DATA.hashbd,DATA.hashkey);
  145. #else
  146.               if (DATA.count && DATA.hashbd == bd && lts (key) == DATA.hashkey)
  147.             {
  148. #endif
  149.                 printf ("%s ", cvt (DATA.bmove,DATA.flags));
  150.                 printf ("%s ", cvt (DATA.hint,0));
  151.                 printf ("%d\n", DATA.count);
  152.             }
  153.           }
  154.         if (in && DATA.bmove)
  155.           {
  156.               c1++;
  157.           }
  158.         else if (DATA.bmove)
  159.           {
  160.               in = 1;
  161.               c1 = 1;
  162.           }
  163.         else if (c1)
  164.           {
  165.               /*printf ("out %d\n", c1);*/
  166.               n++;
  167.               if (c1 < min)
  168.               min = c1;
  169.               if (c1 > max)
  170.               max = c1;
  171.               sum += c1;
  172.               c1 = 0;
  173.               in = 0;
  174.           }
  175.         }
  176.       close (gfd);
  177.       if (in)
  178.         {
  179.         /*printf ("out %d\n", c1);*/
  180.         n++;
  181.         if (c1 < min)
  182.             min = c1;
  183.         if (c1 > max)
  184.             max = c1;
  185.         sum += c1;
  186.         }
  187.       printf ("max %d\nmin %d\navg %f\nsumc %d\n", max, min, (float) sum / (float) n, sumc);
  188.  
  189.       }
  190. }
  191.